package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.adapter.ChooseUserAdapter;
import com.hxkcloud.nurse.adapter.FilterListAdapter;
import com.hxkcloud.nurse.adapter.UsermanagerGridViewAdapter;
import com.hxkcloud.nurse.adapter.UsermanagerGridViewAdapterHuanBing;
import com.hxkcloud.nurse.adapter.UsermanagerGridViewAdapterZhuzhen;
import com.hxkcloud.nurse.beans.DepartmentBean;
import com.hxkcloud.nurse.beans.FilterBean;
import com.hxkcloud.nurse.beans.FilterItem;
import com.hxkcloud.nurse.beans.FilterListBean;
import com.hxkcloud.nurse.beans.FilterPatientBean;
import com.hxkcloud.nurse.beans.UserManagerBean;
import com.hxkcloud.nurse.beans.UserManagerInfo;
import com.hxkcloud.nurse.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.nurse.dborm.DepartmentDao;
import com.hxkcloud.nurse.popup.DatePopWindow;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.utils.StringUtil;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullableRecyclerView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ray_lei on 2016/7/11.
 */
public class ChooseUserActivity extends BaseActivity implements View.OnClickListener, ChooseUserAdapter.ResultSelectedListener, PullToRefreshLayout.OnRefreshListener {

    private LinearLayout ll_title;
    private TextView tv_title;
    private ImageView iv_back;
    private TextView tv_finish;

    private LinearLayout ivgLayout;//导航布局

    private LinearLayout display1Layout;
    private LinearLayout display5Layout;
    private LinearLayout display6Layout;
    private TextView display6TV;
    private TextView display1TV;
    private TextView text_num;
    private ImageView display6IV;
    private ImageView display1IV;
    private View middleView1;
    private View middleView3;

    private DatePopWindow datePopup;
    private String date = "";//时间   格式：时间戳
    private String dateKind = "";//时间类型

    private PopupWindow allPopup;//选择全部
    private PopupWindow showPopWalk;
    private ListView allListView;
    private FilterListAdapter allAdapter;

    private TextView tv_chongzhi;
    private TextView tv_sure;
    private View  viewsback;
    private TextView tv_zhili;
    private TextView tv_zhuzhen;
    private TextView tv_huanbing;
    private GridView gridView_zhili;
    private GridView gridView_zhuzhen;
    private GridView gridView_huanbing;
    private List<FilterItem> list_zhili;
    private List<FilterItem> list_zhuzhen;
    private List<FilterItem> list_huanbing;
//    private List<String> groups;
    private UsermanagerGridViewAdapter gridViewAdapter_zhili;
    private UsermanagerGridViewAdapterZhuzhen gridViewAdapter_zhuzhen;
    private UsermanagerGridViewAdapterHuanBing gridViewAdapter_huanbing;

    private List<FilterPatientBean> filterList_zhili;//筛选条件集合
    private List<FilterPatientBean> filterList_huanbing;//筛选条件集合
    private List<FilterPatientBean> filterList_fenzhu;//筛选条件集合
    private List<String> groups;
    private Map<Integer,Boolean> map_zhili;
    private  Map<Integer,Boolean> map_huanbing;
    private  Map<Integer,Boolean> map_zhuzhen;




    private View allWholeView;


    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private List<UserManagerBean> userManagerInfoList;
    private ChooseUserAdapter adapter;

    private Button allSelectBtn;
    private boolean isAllSelect = false;

    private int pageIndex = 1;
    private int pageSize = 15;
//    private int pageSize = 0;
    private boolean isLoadMore = false;// 是否加载更多

    private ObjectResultBean<UserManagerInfo> resultBean;

    private int totalCount = 0;

    private String statusName = "";

    private boolean isAllClick = false;

    private List<FilterItem> filterItems;

    private ObjectResultBean<FilterListBean> filterBean;

    private List<FilterBean> listBeans;

    private boolean isDisplay1 = false;
    private boolean isDisplay2 = false;
    private boolean isDisplay3 = false;
    private boolean isDisplay4 = false;

    private int clickNum = 1;//1 2 3

    private String display1Default = "";
    private String display2Default = "";
    private String display3Default = "";
    private String display4Default = "";
    private String display1Id = "";
    private String display2Id = "";
    private String display3Id = "";
    private String display4Id = "";
    private String display1Type = "";
    private String display2Type = "";
    private String display3Type = "";
    private String display4Type = "";

    private List<FilterPatientBean> filterList;//筛选条件集合

    private List<String> userIdsList = new ArrayList<>(); //获取服务器用户的id 数组
    private List<String> operationList = new ArrayList<>(); //临时内存调用的  用来操作

    private String isNeedAll = "0";//0是 1否

    private boolean isChanke = false;
    //发送的患者集合
    private ArrayList<String> patients = new ArrayList<>();

    //诸葛记录
    private String isNeedNumber = "";

    String group = "";

    private int first = 4;
    private int second = 6;
    private int third = 1;
    private int status = 0;//住院状态


    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.PREGNANTBMIADD_GETPATIENTFILTERNEWLIST_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    resultBean = (ObjectResultBean<UserManagerInfo>) msg.obj;
                    totalCount = Integer.parseInt(resultBean.getData().getTotalCount());

                    text_num.setText("共" + totalCount + "人");
                    if (resultBean.getData().getList().size() == 0) {
                        if (isLoadMore) {
                            refreshLayout
                                    .loadmoreFinish(PullToRefreshLayout.SUCCEED);
                        } else {
                            userIdsList = resultBean.getData().getAddUserdata();
                            operationList.clear();
                            isAllSelect = false;
                            allSelectBtn.setBackgroundResource((R.mipmap.btn_multiselect));

                            userManagerInfoList.clear();
                            adapter.setData(userManagerInfoList);
                            refreshLayout
                                    .refreshFinish(PullToRefreshLayout.SUCCEED);
                        }
                    } else {
                        if (isLoadMore) {

                        } else {
                            userIdsList = resultBean.getData().getAddUserdata();
                            operationList.clear();
                            isAllSelect = false;
                            allSelectBtn.setBackgroundResource((R.mipmap.btn_multiselect));

                            userManagerInfoList.clear();
                        }
                        userManagerInfoList.addAll(resultBean.getData().getList());

                        adapter.setData(userManagerInfoList);
                        if (isLoadMore) {
                            refreshLayout
                                    .loadmoreFinish(PullToRefreshLayout.SUCCEED);
                        } else {
                            refreshLayout
                                    .refreshFinish(PullToRefreshLayout.SUCCEED);
                        }

                    }


                    for (int i = 0 ; i < userManagerInfoList.size() ; i++)
                    {
                        if(operationList.contains(userManagerInfoList.get(i).getPatientId()))
                        {
                            userManagerInfoList.get(i).setIsSelect(true);
                        }
                    }
                    adapter.notifyDataSetChanged();
                }

                break;
            case MessageCode.PREGNANTBMIADD_GETPATIENTFILTERNEWLIST_FAILURE:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }
                break;

            case MessageCode.PREGNANTBMIADD_GETPATIENTFILTERLIST_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    ivgLayout.setVisibility(View.VISIBLE);
                    filterBean = (ObjectResultBean<FilterListBean>) msg.obj;
                    listBeans = filterBean.getData().getList();
                    if (listBeans != null && listBeans.size() > 0) {
                        for (int i = 0 ; i < listBeans.size() ; i++)
                        {
                            if("状态".equals(listBeans.get(i).getFilterName()))
                            {
                                status = i;
                                //筛选条件 全部 在院 出院
                                display1Default = listBeans.get(i).getFilterName();
                                display1Id = listBeans.get(i).getFilterId();
                                display1Type = listBeans.get(i).getFilterType();
                                if ("1".equals(listBeans.get(i).getFilterType())) {//时间
                                    display1TV.setText(display1Default);
                                } else if ("2".equals(listBeans.get(i).getFilterType())) {
                                    display1TV.setText(listBeans.get(i).getFilterList().get(0).getItemName());
                                    if (isChanke) {

                                    } else {
                                        for (int j = 0; j < listBeans.get(i).getFilterList().size(); j++) {
                                            if (mContext.getResources().getString(R.string.inhospital).equals(listBeans.get(i).getFilterList().get(j).getItemName())) {
                                                listBeans.get(i).getFilterList().get(j).setIsSelect(true);
                                                display1TV.setText(listBeans.get(i).getFilterList().get(j).getItemName());
                                                removeFilterListItem(display1Id);
                                                FilterPatientBean bean = new FilterPatientBean();
                                                bean.setDate("");
                                                bean.setDateKind("");
                                                bean.setFilterId(display1Id);
                                                bean.setFilterName(display1Default);
                                                bean.setFilterType(display1Type);
                                                bean.setItemId(listBeans.get(i).getFilterList().get(j).getItemId());
                                                filterList.add(bean);
                                                break;
                                            }
                                        }
                                    }
                                } else if ("5".equals(listBeans.get(i).getFilterType())) {//自理能力
                                    display1TV.setText(mContext.getResources().getString(R.string.disability_level));
                                } else {
                                    display1TV.setText(listBeans.get(i).getFilterList().get(0).getItemName());
                                }
                            }
                            if("出院时间".equals(listBeans.get(i).getFilterName()))
                            {
                                display4Default = listBeans.get(i).getFilterName();
                                display4Id = listBeans.get(i).getFilterId();
                                display4Type = listBeans.get(i).getFilterType();
                                if ("1".equals(listBeans.get(i).getFilterType())) {//时间
                                } else if ("2".equals(listBeans.get(i).getFilterType())) {
                                    if (isChanke) {

                                    } else {
                                        for (int j = 0; j < listBeans.get(i).getFilterList().size(); j++) {
                                            if (mContext.getResources().getString(R.string.inhospital).equals(listBeans.get(i).getFilterList().get(j).getItemName())) {
                                                removeFilterListItem(display4Id);
                                                FilterPatientBean bean = new FilterPatientBean();
                                                bean.setDate("");
                                                bean.setDateKind("");
                                                bean.setFilterId(display4Id);
                                                bean.setFilterName(display4Default);
                                                bean.setFilterType(display4Type);
                                                bean.setItemId(listBeans.get(i).getFilterList().get(j).getItemId());
                                                filterList.add(bean);
                                                break;
                                            }
                                        }
                                    }
                                } else if ("5".equals(listBeans.get(i).getFilterType())) {//自理能力
                                } else {
                                }
                            }
                        }


                        loading();
                       YiJiaHuNetWorkUtils.getPatientFilterResultList(filterList, "1", "", pageIndex, pageSize, handler);
//                        YiJiaHuNetWorkUtils.getPatientFilterNewResultList(filterList,filterList_fenzhu,filterList_huanbing,filterList_zhili,"1",groups, "", pageIndex, pageSize, handler);

                        //获取filterList里的数据
                        Log.i("listBeans--->","listBeans:"+listBeans.toString());

                        for(int i = 0 ; i < listBeans.size() ; i++)
                        {
                            //自理能力
                            if("自理能力".equals(listBeans.get(i).getFilterName()))
                            {
                                first = i;
                            }
                            //主诊团队
                            if("主诊团队".equals(listBeans.get(i).getFilterName()))
                            {
                                second = i ;
                            }
                            //诊断
                            if("诊断".equals(listBeans.get(i).getFilterName()))
                            {
                                third = i;
                            }
                        }

//                        Log.i("-----sdf--->",list_huanbing.size()+"");
                        for (int i=0;i<listBeans.get(first).getFilterList().size();i++){
                            list_zhili.add(listBeans.get(first).getFilterList().get(i));
                            map_zhili.put(i,false);
//                            Log.i("-----ssss--->",list_huanbing.size()+""+i);
                        }
//                        Log.i("-----listBean4--->",listBeans.get(3).getFilterList().size()+"");
                        for (int i=0;i<listBeans.get(second).getFilterList().size();i++){
                            list_zhuzhen.add(listBeans.get(second).getFilterList().get(i));
                            map_zhuzhen.put(i,false);
                        }

                        for (int i=0;i<listBeans.get(third).getFilterList().size();i++){
                            list_huanbing.add(listBeans.get(third).getFilterList().get(i));
                            map_huanbing.put(i,false);
                        }

                        showPopDate5();
                        tv_zhili.setText(listBeans.get(first).getFilterName());
                        tv_zhuzhen.setText(listBeans.get(second).getFilterName());
                        tv_huanbing.setText(listBeans.get(third).getFilterName());
////                        list_zhuzhen = listBeans.get(3).getFilterList();
                        gridViewAdapter_zhili.notifyDataSetChanged();
                        gridViewAdapter_zhuzhen.notifyDataSetChanged();
                        gridViewAdapter_huanbing.notifyDataSetChanged();

                    } else {
                        ivgLayout.setVisibility(View.GONE);
                        YiJiaHuNetWorkUtils.getPatientFilterResultList(filterList, "1", "", pageIndex, pageSize, handler);
                    }
                }

                break;
            case MessageCode.PREGNANTBMIADD_GETPATIENTFILTERLIST_FAILURE:

                break;

        }

    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_chooseuser);
        userManagerInfoList = new ArrayList<UserManagerBean>();
        listBeans = new ArrayList<>();
        filterItems = new ArrayList<>();
        filterList = new ArrayList<>();

        filterList_zhili = new ArrayList<>();
        filterList_huanbing = new ArrayList<>();
        filterList_fenzhu = new ArrayList<>();
        list_zhuzhen = new ArrayList<>();
        list_huanbing = new ArrayList<>();
        list_zhili = new ArrayList<>();
        groups = new ArrayList<>();
        map_zhili = new HashMap<>();
        map_huanbing = new HashMap<>();
        map_zhuzhen = new HashMap<>();

        if (getIntent().hasExtra("isNeedAll")) {
            isNeedAll = getIntent().getStringExtra("isNeedAll");
        }
        if (getIntent().hasExtra("isNeedNumber")){
            isNeedNumber = getIntent().getStringExtra("isNeedNumber");
        }
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initView() {

        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_finish = (TextView) findViewById(R.id.tv_title_func);
        text_num = (TextView) findViewById(R.id.text_num);
        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.chooseuser_text_title));
        tv_finish.setVisibility(View.VISIBLE);
        tv_finish.setText(getResources().getText(R.string.sure));
        iv_back.setOnClickListener(this);
        tv_finish.setOnClickListener(this);

        ivgLayout = (LinearLayout) findViewById(R.id.chooseuser_layout_ivg);

        display1Layout = (LinearLayout) findViewById(R.id.chooseuser_layout_display1);
        display5Layout = (LinearLayout) findViewById(R.id.chooseuser_layout_display5);
        display6Layout = (LinearLayout) findViewById(R.id.usermanager_layout_display6);
        display1TV = (TextView) findViewById(R.id.chooseuser_tv_display1);
        display6TV = (TextView) findViewById(R.id.usermanager_tv_display4);
        display1IV = (ImageView) findViewById(R.id.chooseuser_iv_display1);
        display6IV = (ImageView) findViewById(R.id.usermanager_iv_display4);
        middleView1 = (View) findViewById(R.id.chooseuser_view_view1);
        middleView3 = (View) findViewById(R.id.chooseuser_view_view3);


        refreshLayout = (PullToRefreshLayout) findViewById(R.id.chooseuser_refreshlayout);
        recycleView = (PullableRecyclerView) findViewById(R.id.chooseuser_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);
        adapter = new ChooseUserAdapter(this, userManagerInfoList, recycleView, this, screenWidth, loginInfo.getDepartmentId());
        recycleView.setAdapter(adapter);

        allSelectBtn = (Button) findViewById(R.id.chooseuser_btn_allselect);

        initAllPopup();


    }
    private void showPopDate5() {
        View views = LayoutInflater.from(mContext).inflate(R.layout.fragment_layout_usermanager_showpop,null);
        viewsback = views.findViewById(R.id.views);
        tv_chongzhi = (TextView) views.findViewById(R.id.fragment_home_textview_chongzhi);
        tv_sure = (TextView) views.findViewById(R.id.fragment_home_textview_sure);
        tv_huanbing = (TextView) views.findViewById(R.id.jibingfenlei_textview);
        tv_zhili = (TextView) views.findViewById(R.id.zhili_textview);
        tv_zhuzhen = (TextView) views.findViewById(R.id.zhuzhen_textview);
        viewsback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopWalk.dismiss();
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                YiJiaHuNetWorkUtils.getPatientFilterNewResultList(filterList,filterList_fenzhu,filterList_huanbing,filterList_zhili,null,null,"1",groups, "", pageIndex, pageSize, handler);
                // YiJiaHuNetWorkUtils.getPatientFilterResultList(filterList, "1", "", pageIndex, pageSize, handler);
                showPopWalk.dismiss();
                loading();

            }
        });
        tv_chongzhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i=0;i<list_zhili.size();i++){
                    map_zhili.put(i,false);
                }
                gridViewAdapter_zhili.setIsSelected(map_zhili);
                for (int i=0;i<list_zhuzhen.size();i++){
                    map_zhuzhen.put(i,false);
                }
                gridViewAdapter_zhuzhen.setIsSelected(map_zhuzhen);
                for (int i=0;i<list_huanbing.size();i++){
                    map_huanbing.put(i,false);
                }
                gridViewAdapter_huanbing.setIsSelected(map_huanbing);

                //groups.removeAll(groups);
                filterList_fenzhu.removeAll(filterList_fenzhu);
                filterList_zhili.removeAll(filterList_zhili);
                filterList_huanbing.removeAll(filterList_huanbing);
            }
        });
//
//
        gridView_zhili = (GridView) views.findViewById(R.id.fragment_zhiligridview);
        gridView_zhuzhen = (GridView) views.findViewById(R.id.fragment_home_zhuzhengridview);
        gridView_huanbing = (GridView) views.findViewById(R.id.fragment_home_huanbinggridview);
        gridViewAdapter_zhili = new UsermanagerGridViewAdapter(mContext,list_zhili);
        gridViewAdapter_zhuzhen = new UsermanagerGridViewAdapterZhuzhen(mContext,list_zhuzhen);
        gridViewAdapter_huanbing = new UsermanagerGridViewAdapterHuanBing(mContext,list_huanbing);
//
        gridView_zhili.setAdapter(gridViewAdapter_zhili);
        gridView_zhuzhen.setAdapter(gridViewAdapter_zhuzhen);
        gridView_huanbing.setAdapter(gridViewAdapter_huanbing);
        gridView_zhili.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //  listBeans.get(1).getFilterList();//诊断1 huanbing
//                        listBeans.get(2).getFilterList();//自理能力2
//                        listBeans.get(3).getFilterList();//主诊团队4
                if (map_zhili.get(i)){
                    map_zhili.put(i,false);
                }else {
                    map_zhili.put(i,true);
                }
                gridViewAdapter_zhili.setIsSelected(map_zhili);
                filterList_zhili.removeAll(filterList_zhili);
                FilterBean  bean =  listBeans.get(first);
                for (int j=0;j<list_zhili.size();j++){
                    if (map_zhili.get(j)){
                        addBean_zhili(bean.getFilterId(),bean.getFilterName(),bean.getFilterType(),bean.getFilterList().get(j).getItemId());
                    }
                }
            }
        });
        gridView_zhuzhen.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                FilterBean  bean =  listBeans.get(second);
                if (map_zhuzhen.get(i)){
                    map_zhuzhen.put(i,false);
                }else {
                    map_zhuzhen.put(i,true);
                }
                filterList_fenzhu.removeAll(filterList_fenzhu);
                gridViewAdapter_zhuzhen.setIsSelected(map_zhuzhen);
                for (int j=0;j<list_zhuzhen.size();j++){

                    if (map_zhuzhen.get(j)){
                      //  groups.add(listBeans.get(3).getFilterList().get(j).getItemId());
                        addBean_fenzhu(bean.getFilterId(),bean.getFilterName(),bean.getFilterType(),bean.getFilterList().get(j).getItemId());
                    }
                }
            }
        });
        gridView_huanbing.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                FilterBean  bean =  listBeans.get(third);
                if (map_huanbing.get(i)){
                    map_huanbing.put(i,false);
                }else {
                    map_huanbing.put(i,true);
                }
                gridViewAdapter_huanbing.setIsSelected(map_huanbing);
                filterList_huanbing.removeAll(filterList_huanbing);
                for (int j=0;j<list_huanbing.size();j++){
                    if (map_huanbing.get(j)){
                        addBean_huanbing(bean.getFilterId(),bean.getFilterName(),bean.getFilterType(),bean.getFilterList().get(j).getItemId());
                    }
                }
            }
        });
        showPopWalk = new  PopupWindow(views, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,true);
        showPopWalk.setOutsideTouchable(true);
        showPopWalk.setTouchable(true);
        showPopWalk.setFocusable(true);
        showPopWalk.setBackgroundDrawable(new ColorDrawable(0x22000000));
        showPopWalk.setAnimationStyle(R.style.popwindow_anim_right);
        }

    private void addBean_fenzhu(String filterId,String filterName,String filterType,String itemId) {
        FilterPatientBean bean = new FilterPatientBean();
        bean.setDate("");
        bean.setDateKind("");
        bean.setFilterId(filterId);
        bean.setFilterName(filterName);
        bean.setFilterType(filterType);
        bean.setItemId(itemId);
        filterList_fenzhu.add(bean);
    }
    private void addBean_zhili(String filterId,String filterName,String filterType,String itemId) {
        FilterPatientBean bean = new FilterPatientBean();
        bean.setDate("");
        bean.setDateKind("");
        bean.setFilterId(filterId);
        bean.setFilterName(filterName);
        bean.setFilterType(filterType);
        bean.setItemId(itemId);
        filterList_zhili.add(bean);
    }
    private void addBean_huanbing(String filterId,String filterName,String filterType,String itemId) {
        FilterPatientBean bean = new FilterPatientBean();
        bean.setDate("");
        bean.setDateKind("");
        bean.setFilterId(filterId);
        bean.setFilterName(filterName);
        bean.setFilterType(filterType);
        bean.setItemId(itemId);
        filterList_huanbing.add(bean);
    }

    @Override
    protected void initListener() {
        iv_back.setOnClickListener(this);
        tv_finish.setOnClickListener(this);
        allSelectBtn.setOnClickListener(this);

        display1Layout.setOnClickListener(this);
        display5Layout.setOnClickListener(this);
        display6Layout.setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected void initData() {
        DepartmentDao dao = new DepartmentDao(mContext);
        List<DepartmentBean> beans = dao.getDepartment();
        if (beans != null) {
            for (int i = 0; i < beans.size(); i++) {
                if ((beans.get(i).getDepartmentId() + "").equals(loginInfo.getDepartmentId())) {
                    if ("0".equals(beans.get(i).getType())) {
                        isChanke = true;
                    }
                    break;
                }
            }
        }

        loading();
        YiJiaHuNetWorkUtils.getPatientFilterList(loginInfo.getDepartmentId(), handler);
     //   YiJiaHuNetWorkUtils.getPatientFilterNewResultList(filterList,filterList_fenzhu,filterList_huanbing,filterList_zhili,"1",groups, "", pageIndex, pageSize, handler);
        pageIndex = 1;
    }

    private void initAllPopup() {
        allPopup = new PopupWindow(this);
        View allView = LayoutInflater.from(this).inflate(R.layout.popup_layout_kind, null);
        allPopup.setContentView(allView);

        allPopup.setWidth(screenWidth);
        allPopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        allPopup.setFocusable(true);
        allPopup.setTouchable(true);
        allPopup.setOutsideTouchable(true);
        allPopup.setBackgroundDrawable(new ColorDrawable(0x00000000));


        allListView = (ListView) allView.findViewById(R.id.kind_listview);
        allAdapter = new FilterListAdapter(this, filterItems);
        allListView.setAdapter(allAdapter);
        allListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < filterItems.size(); i++) {
                    if (i == position) {
                        filterItems.get(i).setIsSelect(true);
                        statusName = filterItems.get(i).getItemName();
                    } else {
                        filterItems.get(i).setIsSelect(false);
                    }
                }

                FilterPatientBean bean = new FilterPatientBean();
                bean.setDate("");
                bean.setDateKind("");
                if (clickNum == 1) {
                    isDisplay1 = true;
//                    display1TV.setText(filterItems.get(position).getItemName());
                    String name = filterItems.get(position).getItemName();
                    if ("5".equals(display1Type) && "-1".equals(filterItems.get(position).getItemId())) {
                        display1TV.setText(mContext.getResources().getString(R.string.disability_level));
                    } else {
                        if (name.length() > 4) {
                            String displayName = name.substring(0, 3);
                            display1TV.setText(displayName + "...");
                        } else {
                            display1TV.setText(name);
                        }
                    }

                    if ("2".equals(display1Type) && !"in".equals(filterItems.get(position).getItemId())) {
                        isDisplay1 = true;
                        isDisplay2 = true;
                        isDisplay3 = true;
                        isDisplay4 = true;
                        display1Layout.setEnabled(true);
                    }
                    if ("2".equals(display1Type) && "in".equals(filterItems.get(position).getItemId())) {
                        if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display1Default)) {
                            display1TV.setText(mContext.getResources().getString(R.string.usermanger_text_time));
                        } else if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display2Default)) {
                        } else if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display3Default)) {
                        } else if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display4Default)) {
                        }
                        removeFilterTimeItem();
                    }

                    display1IV.setImageResource(R.mipmap.tab_arrow);
                    removeFilterListItem(display1Id);
                    bean.setFilterId(display1Id);
                    bean.setFilterName(display1Default);
                    bean.setFilterType(display1Type);
                    bean.setItemId(filterItems.get(position).getItemId());

                } else if (clickNum == 2) {
                    isDisplay2 = true;
                    String name = filterItems.get(position).getItemName();
                    if ("5".equals(display2Type) && "-1".equals(filterItems.get(position).getItemId())) {
                    } else {
                        if (name.length() > 4) {
                            String displayName = name.substring(0, 3);
                        } else {
                        }
                    }
                    if ("2".equals(display2Type) && !"in".equals(filterItems.get(position).getItemId())) {
                        isDisplay1 = true;
                        isDisplay4= true;

                        display1Layout.setEnabled(true);
                        isDisplay2 = true;
                        isDisplay3 = true;

                    }
                    if ("2".equals(display1Type) && "in".equals(filterItems.get(position).getItemId())) {
                        if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display1Default)) {
                            display1TV.setText(mContext.getResources().getString(R.string.usermanger_text_time));
                        } else if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display2Default)) {
                        } else if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display3Default)) {
                        } else if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display4Default)) {
                        }
                        removeFilterTimeItem();
                    }
                    removeFilterListItem(display2Id);
                    bean.setFilterId(display2Id);
                    bean.setFilterName(display2Default);
                    bean.setFilterType(display2Type);
                    bean.setItemId(filterItems.get(position).getItemId());
                } else if (clickNum == 3) {
                    isDisplay3 = true;
                    String name = filterItems.get(position).getItemName();
                    if ("5".equals(display3Type) && "-1".equals(filterItems.get(position).getItemId())) {
                    } else {
                        if (name.length() > 4) {
                            String displayName = name.substring(0, 3);
                        } else {
                        }
                    }
                    if ("2".equals(display3Type) && !"in".equals(filterItems.get(position).getItemId())) {
                        isDisplay1 = true;
                        isDisplay2 = true;
                        isDisplay3 = true;
                        isDisplay4 = true;
                        display1Layout.setEnabled(true);
                    }
                    if ("2".equals(display1Type) && "in".equals(filterItems.get(position).getItemId())) {
                        if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display1Default)) {
                            display1TV.setText(mContext.getResources().getString(R.string.usermanger_text_time));
                        } else if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display2Default)) {
                        } else if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display3Default)) {
                        } else if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display4Default)) {
                        }
                        removeFilterTimeItem();
                    }
                    removeFilterListItem(display3Id);
                    bean.setFilterId(display3Id);
                    bean.setFilterName(display3Default);
                    bean.setFilterType(display3Type);
                    bean.setItemId(filterItems.get(position).getItemId());
                } else if (clickNum == 4) {
                    isDisplay4 = true;
                    String name = filterItems.get(position).getItemName();
                    if ("5".equals(display4Type) && "-1".equals(filterItems.get(position).getItemId())) {
                    } else {
                        if (name.length() > 4) {
                            String displayName = name.substring(0, 3);
                        } else {
                        }
                    }
                    if ("2".equals(display4Type) && !"in".equals(filterItems.get(position).getItemId())) {
                        isDisplay1 = true;
                        isDisplay2 = true;
                        isDisplay3 = true;
                        isDisplay4 = true;
                        display1Layout.setEnabled(true);
                    }
                    if ("2".equals(display1Type) && "in".equals(filterItems.get(position).getItemId())) {
                        if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display1Default)) {
                            display1TV.setText(mContext.getResources().getString(R.string.usermanger_text_time));
                        } else if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display2Default)) {
                        } else if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display3Default)) {
                        } else if (mContext.getResources().getString(R.string.usermanger_text_time).equals(display4Default)) {
                        }
                        removeFilterTimeItem();
                    }
                    removeFilterListItem(display4Id);
                    bean.setFilterId(display4Id);
                    bean.setFilterName(display4Default);
                    bean.setFilterType(display4Type);
                    bean.setItemId(filterItems.get(position).getItemId());

                    //修改过
                   // group = listBeans.get(3).getFilterList().get(position).getItemId();
                }
                filterList.add(bean);

                allAdapter.notifyDataSetChanged();
                allPopup.dismiss();
                loading();
                //4.17日
//                if(!"".equals(group)){
////                    YiJiaHuNetWorkUtils.getPatientFilterNewResultList(filterList, "1", groups,"", pageIndex, pageSize, handler);
//                    YiJiaHuNetWorkUtils.getPatientFilterResultList(filterList, isNeedAll, "", pageIndex, pageSize, handler);
//
//                }else {
//                    YiJiaHuNetWorkUtils.getPatientFilterResultList(filterList, isNeedAll, "", pageIndex, pageSize, handler);
//                }

                YiJiaHuNetWorkUtils.getPatientFilterNewResultList(filterList,filterList_fenzhu,filterList_huanbing,filterList_zhili,null,null,"1",groups, "", pageIndex, pageSize, handler);
            }
        });
        allAdapter.setDisplayListener(new FilterListAdapter.DisplayListener() {
            @Override
            public void onDisplay(int position) {

            }
        });

        allWholeView = (View) allView.findViewById(R.id.kind_view_whole);
        allWholeView.setOnClickListener(this);

        allPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                display1IV.setImageResource(R.mipmap.tab_arrow);
            }
        });
    }

    private void removeFilterTimeItem() {
        if (filterList != null && filterList.size() > 0) {
            for (int i = 0; i < filterList.size(); i++) {
                if (filterList.get(i).getFilterName().equals(mContext.getResources().getString(R.string.usermanger_text_time))) {
                    filterList.remove(i);
                }
            }
        }
    }

    private void setInitPopup() {

        display1TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
        display1IV.setImageResource(R.mipmap.tab_arrow);

        display6TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
        display6IV.setImageResource(R.mipmap.tab_arrow);

    }

    private void removeFilterListItem(String kindId) {
        if (filterList != null && filterList.size() > 0) {
            for (int i = 0; i < filterList.size(); i++) {
                if (kindId.equals(filterList.get(i).getFilterId())) {
                    filterList.remove(i);
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.all_view_whole:
                if (allPopup != null) {
                    allPopup.dismiss();
                }
                break;
            case R.id.kind_view_whole:
                display1IV.setImageResource(R.mipmap.tab_arrow);
                allPopup.dismiss();

                break;
            case R.id.chooseuser_layout_display1:
                setInitPopup();
                clickNum = 1;
                display1IV.setImageResource(R.mipmap.tab_arrow_down_grenn);
                filterItems = listBeans.get(status).getFilterList();
                allAdapter.setData(filterItems);
                allPopup.showAsDropDown(ivgLayout);
                break;
            case R.id.chooseuser_layout_display5:
               // toastMsg("筛选有效");
                    display6IV.setImageResource(R.mipmap.tab_arrow);
                    showPopWalk.showAtLocation(ll_title, Gravity.RIGHT,0,0);
                break;
            case R.id.usermanager_layout_display6:

                setInitPopup();
                if (!"在院".equals(display1TV.getText().toString())){
                display6IV.setImageResource(R.mipmap.tab_arrow_up_green);
                showPopDate4();
                 }
                break;
            case R.id.iv_title_back:
                finish();
                break;

            case R.id.tv_title_func:
                if ("0".equals(isNeedNumber)){
                    StringUtil.zhugeTrack(mContext, "量表-用户自评-选择用户-确定按钮" ,  "量表助手使用次数");
                }
                if ("1".equals(isNeedNumber)){
                    StringUtil.zhugeTrack(mContext, "调查问卷-用户自评-选择用户-确定按钮" , "调查问卷助手使用次数");
                }
                patients.clear();
                for (String userId : operationList) {
                    patients.add(userId);
                }

                if (patients.size() == 0) {
                toastMsg(R.string.pleasechoosepatients);
                return;
            }

                Log.i("--patients-->","size:"+patients.size()+"obj:"+patients.toString());
                Intent iBack = new Intent();
                iBack.putStringArrayListExtra("patients", patients);
                setResult(RESULT_OK, iBack);
                finish();
 /*               OpenMyPopWindowUtils.confirm(ChooseUserActivity.this, R.id.tv_title_func, "确定要发送吗?", new IReturnTrueOrFalse() {
                    @Override
                    public void returnBoolean(boolean isOk) {
                        if (isOk) {
                            Intent iBack = new Intent();
                            iBack.putStringArrayListExtra("patients", patients);
                            setResult(RESULT_OK, iBack);
                            finish();
                        }
                    }
                });*/

                break;
            case R.id.chooseuser_btn_allselect://全选
                if (isAllSelect) {
                    allSelectBtn.setBackgroundResource(R.mipmap.btn_multiselect);
                    operationList.clear();

                    for (int i = 0; i < userManagerInfoList.size(); i++) {
                        userManagerInfoList.get(i).setIsSelect(false);
                    }

                    isAllSelect = false;
                } else {
                    allSelectBtn.setBackgroundResource(R.mipmap.btn_multiselect_pre);
                    operationList.clear();
                    operationList.addAll(userIdsList);

                    for (int i = 0; i < userManagerInfoList.size(); i++) {
                        userManagerInfoList.get(i).setIsSelect(true);
                    }
                    isAllSelect = true;
                }
                adapter.notifyDataSetChanged();


                break;

        }
    }

    private void showPopDate4() {
        if (datePopup == null) {
            datePopup = new DatePopWindow(this);
            datePopup.setTimeListener(new DatePopWindow.TimeListener() {
                @Override
                public void setTime(String time, String typeDate) {
                    if ("".equals(time)) {
                        if ("dismiss".equals(typeDate)) {
                            display1IV.setImageResource(R.mipmap.tab_arrow);
                            display6IV.setImageResource(R.mipmap.tab_arrow);
                        } else {
                            display1IV.setImageResource(R.mipmap.tab_arrow);
                            display6IV.setImageResource(R.mipmap.tab_arrow);
                            display6TV.setText("出院时间");
                        }
                        date = "";
                        dateKind = "";
                        //removeFilterListItem(display4Id);
                        removeFilterListItem("52");
                        FilterPatientBean bean = new FilterPatientBean();
                        bean.setDate("");
                        bean.setDateKind("");
//                      bean.setFilterId(display4Id);
                        bean.setFilterId("52");
                        bean.setFilterName("出院时间");
                        bean.setFilterType("1");
                        bean.setItemId("");

                        loading();
                        filterList.add(bean);
                        // YiJiaHuNetWorkUtils.getPatientFilterResultList(filterList, "1", "", pageIndex, pageSize, handler);
                        YiJiaHuNetWorkUtils.getPatientFilterNewResultList(filterList,filterList_fenzhu,filterList_huanbing,filterList_zhili,null,null,"1",groups, "", pageIndex, pageSize, handler);
                        display6IV.setImageResource(R.mipmap.tab_arrow);
                        return;
                    }
                    dateKind = typeDate;

                    if ("0".equals(typeDate)) {
                        SimpleDateFormat df = new SimpleDateFormat("yyyy");
                        try {
                            date = df.parse(time).getTime() + "";
                        } catch (ParseException e) {

                        }
                    } else if ("1".equals(typeDate)) {
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
                        try {
                            date = df.parse(time).getTime() + "";
                        } catch (ParseException e) {

                        }
                    } else if ("2".equals(typeDate)) {
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        try {
                            date = df.parse(time).getTime() + "";
                        } catch (ParseException e) {

                        }
                    } else {
                        date = "";
                    }

                    loading();
                    //日期 filterId = 52
//                    removeFilterListItem(display4Id);
                    removeFilterListItem("52");
                    FilterPatientBean bean = new FilterPatientBean();
                    bean.setDate(date);
                    bean.setDateKind(dateKind);
//                    bean.setFilterId(display4Id);
                    bean.setFilterId("52");
                    bean.setFilterName("出院日期");
//                    bean.setFilterType(display4Type);
                    bean.setFilterType("1");
                    bean.setItemId("");

                    filterList.add(bean);
                    //4.17日
                   // YiJiaHuNetWorkUtils.getPatientFilterResultList(filterList, "1", "", pageIndex, pageSize, handler);
                    YiJiaHuNetWorkUtils.getPatientFilterNewResultList(filterList,filterList_fenzhu,filterList_huanbing,filterList_zhili,null,null,"1",groups, "", pageIndex, pageSize, handler);
                    String displayTime = time.replaceAll("-", "/");
                    if (displayTime.length() > 7) {
                        String displayDate = displayTime.substring(2, displayTime.length());
                        display6TV.setText(displayDate);
                        display6IV.setImageResource(R.mipmap.tab_arrow);
                    } else {
                        display6TV.setText(displayTime);
                        display6IV.setImageResource(R.mipmap.tab_arrow);
                    }
                }
            });
        }
        datePopup.showAsDropDown(ivgLayout);
    }

    @Override
    public void onSelected(String type, int position) {

        if ("select".equals(type)) {

            operationList.add(userManagerInfoList.get(position).getPatientId());

            userManagerInfoList.get(position).setIsSelect(true);
            adapter.notifyItemChanged(position);

            boolean isAllSelectS = true;
            if(operationList.size() != userIdsList.size())
            {
                isAllSelectS = false;
            }

            if (isAllSelectS) {
                allSelectBtn.setBackgroundResource(R.mipmap.btn_multiselect_pre);
                isAllSelect = true;
            }
        } else if ("no_select".equals(type)) {

            operationList.remove(userManagerInfoList.get(position).getPatientId());

            userManagerInfoList.get(position).setIsSelect(false);
            adapter.notifyItemChanged(position);

            boolean isAllSelectNoS = false;
            Log.i("--DDD-->",operationList.size()+"-"+userIdsList.size());
            if(operationList.size() != userIdsList.size())
            {
                isAllSelectNoS = true;
            }

            if (isAllSelectNoS) {
                allSelectBtn.setBackgroundResource(R.mipmap.btn_multiselect);
                isAllSelect = false;
            }
        }
       /* int num = 0;
        for (int i = 0; i < userManagerInfoList.size(); i++) {
            if (userManagerInfoList.get(i).isSelect()) {
                num++;
            }
        }
        text_num.setText("共" + num + "人");*/

    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        isLoadMore = false;
        pageIndex = 1;
        userManagerInfoList.clear();
        operationList.clear();


        isAllSelect = false;
        allSelectBtn.setBackgroundResource((R.mipmap.btn_multiselect));
        //4.17日
      //YiJiaHuNetWorkUtils.getPatientFilterResultList(filterList, isNeedAll, "", pageIndex, pageSize, handler);
        YiJiaHuNetWorkUtils.getPatientFilterNewResultList(filterList,filterList_fenzhu,filterList_huanbing,filterList_zhili,null,null,"1",groups, "", pageIndex, pageSize, handler);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

        if (userManagerInfoList.size() == totalCount) {
            isLoadMore = false;
            refreshLayout
                    .loadmoreFinish(PullToRefreshLayout.SUCCEED);
            return;
        }

//        isAllSelect = false;
//        allSelectBtn.setBackgroundResource((R.mipmap.btn_multiselect));

        isLoadMore = true;
        pageIndex++;
        //YiJiaHuNetWorkUtils.getPatientFilterResultList(filterList, isNeedAll, "", pageIndex, pageSize, handler);
        YiJiaHuNetWorkUtils.getPatientFilterNewResultList(filterList,filterList_fenzhu,filterList_huanbing,filterList_zhili,null,null,"1",groups, "", pageIndex, pageSize, handler);
    }
}
